import { _decorator, Component, Node, ProgressBar } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('player')
export class player extends Component {
    @property
    public maxHp: number = 100;     // 最大生命值
    @property
    public maxStm: number = 100;        // 最大体力值
    @property
    public gainStmTime: number = 1.5;   // 恢复体力所需时间
    @property
    public gainStmAmount: number = 5;   // 每秒恢复体力数量
    @property
    public maxAmmo: number = 300;       // 最大子弹数量
    @property
    public speed: number = 5;       // 移动速度
    @property
    public fastSpeedRatio: number = 1.5;        // 快速移动倍数
    @property
    public basicAttack = 10;        // 基础伤害
    @property
    public basicDefence = 10;       // 基础防御

    private hp: number;     // 当前生命值
    private stm: number; // 当前体力值
    private gainStm: boolean = false;   // 是否需要恢复体力
    private gainStmTimer: number = 0;   // 恢复体力计时
    private ammo: number;   // 当前弹药
    private attack: number;     // 当前伤害值
    private defence: number;    // 当前防御值
    private dead: boolean = false;      // 是否死亡

    @property(Node)
    public stmBar: Node = null!;      // 体力槽
    private bar: ProgressBar = null;

    onLoad() {
        this.init();
    }

    init() {
        // 初始化属性值
        this.hp = this.maxHp;
        this.stm = this.maxStm;
        this.ammo = this.maxAmmo;
        this.attack = this.basicAttack;
        this.defence = this.basicDefence;
        // 设置组件
        if (this.stmBar != null) {
            this.bar = this.stmBar.getComponent(ProgressBar);
        }
    }

    start() {

    }

    update(deltaTime: number) {
        // 自动回复体力
        if (this.gainStmTimer <= 0) {
            this.gainStamina(this.gainStmAmount * deltaTime);
            this.gainStmTimer = 0;
        } else {
            this.gainStmTimer -= 1 * deltaTime;
        }

        this.setUIState();
    }

    // 受伤害
    public takeDamage(dmg: number) {

    }

    // 消耗体力
    public costStamina(stm: number) {
        if (this.stm - stm > 0) {
            this.stm -= stm;
        } else {
            this.stm = 0
        }

        // 设置恢复体力计时器
        this.gainStm = true;
        this.gainStmTimer = this.gainStmTime;
    }

    // 恢复体力
    public gainStamina(stm: number = 5) {
        if (this.gainStm) {
            if (this.stm + stm < this.maxStm) {
                this.stm += stm;
            } else {
                this.stm = this.maxStm;
                this.gainStm = false;
            }
        }
    }

    // 设置UI组件的状态
    setUIState() {
        // 设置体力槽
        if (this.bar != null) {
            this.bar.progress = this.stm / this.maxStm;
        }
    }
}


